<template>
    <el-row class=" min-h-screen">
        <el-col :lg="16" :md="12" class="left-container">
            <div class="text-light-50 flex flex-col justify-center items-center">
                <div class=" font-bold text-5xl mb-4">欢迎光临</div>
                <div>人工智能学院，大数据专业出品</div>
            </div>
        </el-col>
        <el-col :lg="8" :md="12" class="bg-light-50 flex flex-col justify-center items-center">
            <h2 class="text-gray-800 text-3xl font-bold">欢迎回来</h2>
            <div class="flex items-center justify-center my-5 text-gray-300 space-x-2">
                <span class="h-[1px] w-16 bg-gray-200"></span>
                <span>账号密码登录</span>
                <span class="h-[1px] w-16 bg-gray-200"></span>
            </div>
            <el-form :model="form" ref="formRef" :rules="rules" class=" w-[250px]">
                <el-form-item prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名">
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" show-password v-model="form.password" placeholder="请输入密码">
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <User />
                            </el-icon>
                        </template></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button color="#626aef" round type="primary" class="w-[250px]" @click="onSubmit"
                        :loading="loading">登录</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>

</template>

<script setup>
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from "@/axios/manager"
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import {setToken} from '@/composables/auth'

const router = useRouter()

const form = reactive({
    username: "admin",
    password: "admin"
})

const rules = {
    username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
        { min: 3, max: 10, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    password: [

    ]
}

const formRef = ref(null)
const loading = ref(false)

const onSubmit = () => {
    formRef.value.validate((valid => {
        if (!valid) {
            return false
        } else {
            loading.value = true
            //对接后端登录接口
            login(form.username, form.password)
                .then(res => {
                    //存储token
                    setToken(res.token)
                    //跳转到首页
                    router.push("/")
                }).finally(() => {
                    loading.value = false
                })
        }
    }))
}
</script>

<style scoped>
.left-container {
    @apply bg-indigo-500 flex justify-center items-center
}
</style>